package uf.tax.laws.spider.sd;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import uf.tax.laws.spider.HttpDAO;

/**
 * 获取法律法规列表
 * 
 * @author sunny
 *
 */
public class GetLawList {
	private HttpDAO http;

	public GetLawList(HttpDAO http) {
		this.http = http;
	}

	Pattern _pattern = Pattern.compile(">.*?共 (.*?) 个", Pattern.DOTALL);

	public int getItemCountByCatId(String catId) {
		String resultContent = http.get(String.format(
				"http://www.taxad.cn/news/newstsort.aspx?catid=%s", catId));
		Matcher matcher = _pattern.matcher(resultContent);
		if (matcher.find()) {
			return Integer.valueOf(matcher.group(1));
		}
		return 15;
	}

	static Pattern pattern = Pattern
			.compile(
					"font-size:14px; width:688px\"><a href=\"(.*?)\".*?title=\"(.*?)\"",
					Pattern.DOTALL);

	public Map<String, String> getItems(String catId, int page) {
		// href="/news/detail/55544.html" target="_blank"
		// title="国家税务总局关于《中华人民共和国和德意志联邦共和国对所得和财产避免双重征税和防止偷漏税的协定》及议定书生效执行的公告">
		String resultContent = http.get(String.format(
				"http://www.taxad.cn/news/newstsort.aspx?catid=%s&page=%d",
				catId, page));
		Matcher matcher = pattern.matcher(resultContent);
		Map<String, String> resultMap = new HashMap<String, String>();
		while (matcher.find()) {
			resultMap.put(matcher.group(2),
					"http://www.taxad.cn" + matcher.group(1));
		}
		return resultMap;
	}
}
